<!--
 * @Author: @yzcheng
 * @Date: 2021-04-12 17:37:49
 * @Version: 1.0
 * @LastEditors: @yzcheng
 * @Description: 
 * @LastEditTime: 2021-06-24 10:50:21
-->
<template>
  <div class="process_form">
    <div class="process_form-item">
      <slot />
    </div>
    <div v-if="!parent.type && parent.labelType!='doned'" class="process_task">
      <div class="process_copy">
        <el-form ref="form" size="small" label-width="80px" :rules="rules">
          <el-form-item
            v-if="parent.flowData.authorityList?.find((item) => item.name === '抄送')"
            :label="'抄送人'"
          >
            <el-input v-model="ccText" @click="getCc" readonly>
              <template #suffix>
                <i class="el-icon-search el-input__icon"></i>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item v-if="parent.flowData.authorityList" :label="'审批意见'">
            <el-input v-model="comment" type="textarea"></el-input>
          </el-form-item>
          <el-form-item>
            <div class="form_button">
              <div class="form_button_item" v-for="{ key, type, title } in buttonList" :key="key">
                <el-button
                  v-if="parent.flowData.authorityList?.find((item) => item.name === title)"
                  size="small"
                  :type="type"
                  plain
                  @click="Task(key, title)"
                  >{{ title }}</el-button
                >
              </div>
            </div>
          </el-form-item>
        </el-form>
      </div>
      <UserTaskGeneral :radio="radio" :getUserInfo="getUserInfo" v-model:dialogUser="dialogUser" />
      <Complaint
        @submit="submit"
        :info="info"
        :userName="userName"
        v-model:radio="radio"
        v-model:dialogUser="dialogUser"
        v-model:visible="visible"
      />
    </div>
  </div>
</template>

<script>
import { ElMessage } from 'element-plus'
import {
add,
back,
back_first,
completeTaskByVo,
delegateTask,
opinionAddComment,
opinionComplete,
opinionSave,
over,
reject,
terminate,
trunTask,
} from '../utils/api'
import Complaint from './complaint.vue'

import UserTaskGeneral from '../ChooseUser/UserTask_General.vue'
export default {
  components: {
    Complaint,
    UserTaskGeneral,
  },

  props: {
    proDefId: String,
    processInstanceId: String,
  },
  inject: ['parent'],
  data() {
    return {
      CcPeople: [],
      userName: '',
      visible: false,
      radio: false,
      dialogUser: false,
      buttonList: [
        { type: 'success', title: '通过', key: 1 },
        { type: 'info', title: '退回', key: 2 },
        { type: 'danger', title: '退回起点', key: 3 },
        { type: 'danger', title: '退回上一步', key: 4 },
        { type: 'danger', title: '强制办结', key: 5 },
        { type: 'primary', title: '转办', key: 6 },
        { type: 'success', title: '委派', key: 7 },
        { type: 'danger', title: '废弃', key: 8 },
        { type: 'success', title: '意见征询', key: 9 },
        { type: 'success', title: '提交意见', key: 10 },
        { type: 'danger', title: '驳回', key: 11 },
      ],
      info: {},
      options: [],
      comment: '',
      ccText: '',
    }
  },
  mounted(){
    console.log("审批",this.parent);
    
  },
  methods: {
    getCc() {
      this.dialogUser = true
      this.radio = false
    },
    getUserInfo(val, user) {
      if (this.radio) {
        this.userName = ''
      } else {
        this.ccText = ''
      }
      this.CcPeople = user.filter((item) => val.includes(item.key))
      if (!this.radio) {
        if (this.info.type === 9) {
          this.userName = this.CcPeople.map((item) => item.cname).join(',')
        } else {
          this.ccText = this.CcPeople.map((item) => item.cname).join(',')
        }
      } else {
        this.userName = this.CcPeople?.[0]?.cname
      }
    },
    async submit(type) {
      const { comment } = this
      const {
        taskId,
        processDefinitionId,
        processInstanceId,
        bussinessId,
        oid,
      } = this.parent.flowData
      this.visible = false
      let data = new FormData()
      let rst = null
      let addMsg = null
     
      
      const res = await opinionComplete({ taskId })
      if (!res?.data || type === 10) {
        switch (type) {
          case 1:
            if (this.parent.onSubmit) {
              rst = await completeTaskByVo({
                comment,
                procInsId: processInstanceId,
                taskId,
                map: this.parent.onSubmit(),
              })
            } else {
              rst = await completeTaskByVo({
                comment,
                procInsId: processInstanceId,
                taskId,
              })
            }

            break
          case 2:
            break
          case 3:
            data.append('currentTaskId', taskId)
            data.append('comment', comment)
            rst = await back_first(data)
            break
          case 4:
            data.append('currentTaskId', taskId)
            data.append('comment', comment)
            rst = await back(data)
            break
          case 5:
            data.append('currentTaskId', taskId)
            data.append('comment', comment)
            rst = await over(data)
            break
          case 6:
            rst = await trunTask({
              procInsId: processInstanceId,
              taskId,
              comment,
              userCode: this.userName.key,
            })
            break
          case 7:
            rst = await delegateTask({
              procInsId: processInstanceId,
              taskId,
              comment,
              userCode: this.userName.key,
            })
            break
          case 8:
            data.append('processInstanceId', processInstanceId)
            data.append('comment', comment)
            data.append('status', 2)
            rst = await terminate(data)
            break
          case 9:
            rst = await opinionSave({
              bussinessId,
              procInsId: processInstanceId,
              taskId,
              usernames: this.CcPeople.map((item) => {
                return `${item.cname || ''}:${item.key || ''}`
              }),
            })
            break
          case 10:
            data.append('comment', comment)
            data.append('id', oid)
            rst = await opinionAddComment(data)
            break
          case 11:
            data.append('currentTaskId', taskId)
            data.append('comment', comment)
            data.append('status', 8)
            rst = await reject(data)
            break
          default:
            break
        }
        if (rst.code === 200) {
          ElMessage.success(this.info.title + '处理成功')
          if (this.CcPeople.length) {
            const userCodeList = this.CcPeople.map((item) => item.key)
            addMsg = add({ processDefinitionId, processInstanceId, userCodeList, taskId })
          }
        } else {
          ElMessage.error('办理失败，请联系管理员')
        }
      } else {
        ElMessage.error('当前流程有未处理的意见征询')
      }
      if (this.parent.onSuccessful) {
        this.parent.onSuccessful(rst, addMsg)
      }
    },
    Task(type, title) {
      this.info = {
        type,
        title,
      }
      this.userName = ''
      this.visible = true
    },
  },
}
</script>

<style lang="scss" scoped>
.process_form {
  display: flex;
  flex-direction: column;
  & > div {
    padding-bottom: 20px;
  }
  .process_form-item {
    flex: 1;
    // 布局受到影响，暂时注释
    // display: flex;
    flex-direction: column;
    align-items: center;
    min-height: 500px;
    max-height: 500px;
    overflow: auto;
  }
  .process_task {
    box-shadow: 0px -4px 3px rgba(0, 0, 0, 0.2);
    padding-top: 10px;
    max-height: 165px;
    .process_copy {
      width: 800px;
      margin: auto;
      .form_button {
        display: flex;
        justify-content: flex-start;
        & > div > button {
          margin-right: 10px;
        }
      }
    }
  }
}
</style>
